var main = {
    manageId: 1,
    userType: 1,
    submitId: "",
    init: function() {
        if (GetQueryString("userId")) {
            this.manageId = GetQueryString("userId"); //获取当前会话的id
        }
        if (GetQueryString("type")) {
            this.userType = GetQueryString("type"); //获取当前的用户类型
        }



        //this.initBtn();
        this.bindDetaEvent(); //详情页相关事件
        this.bindWorkerEvent(); //兼职列表相关事件
    },
    getCountInfo: function() {
        var thiz = this;
        start.postAjaxReq({
            param: {
                "id": thiz.manageId,
                "task_id": $("#J-desc-id").data("id"),
                "type": thiz.userType
            },
            type: "GET",
            url: "/task/joinNum",
            success: function(data) {
                console.log(data);
            }
        })
    },
    _showMask: function(str, cb) {
        var arr = [];
        arr.push('<div id="J_mask" class="window_rgb">');
        arr.push('<div class="window_box">');
        arr.push('<i class="right_ico"></i>');
        arr.push('<span>' + str + '</span>');
        arr.push('</div></div>');
        $("body").append(arr.join(""));
        $("#J_mask").show();
        setTimeout(function() {
            $("#J_mask").remove();
            if (cb) {
                cb();
            }

        }, 2000);
    },
    _alertMask: function(str, type) {
        var arr = [];
        arr.push('<div id="J_alert" class="window_rgb">');
        arr.push('<div class="window_box">');
        arr.push('<span class="sure_or_no">' + str + '</span>');
        arr.push('<div class="confirm clearfix">');
        arr.push('<a class="j_sure sure" href="javascript:;" type="' + type + '">是</a>');
        arr.push('<a class="j_no" href="javascript:;">否</a>');
        arr.push('</div></div></div>');
        $("body").append(arr.join(""));
        $("#J_alert").show();
    },
    //获取支付信息
    getcashInfo: function() {
        var id = "",
            arr = [];
        var count = 0;
        var no = 0;
        $(".texts_box:visible").find("li").each(function() {
            var now = $(this);
            if (now.find(".checkR").is(":visible")) {
                arr.push(now.data("id"));
                count = count + parseInt(now.find(".salarys").html());
                no++;
            }
        });
        this.submitId = arr.join(",");
        return {
            cash: count,
            no: no
        };
    },
    bindDetaEvent: function() {
        var thiz = this;
        $(".footer li").delegate('.checkBox_ico ,.all_select', 'click', function() {
            var _first = $(this).parent('li').children('.checkBox_ico');
            var _parents = $(this).parents(".footer").siblings(".mid_content").find("li").find(".list_checkBox_ico");

            if (_first.children('.active_check').css("display") == "none") {
                _first.find('i').addClass('active_check').show();
                _parents.find("i").addClass("checkR").show();

            } else {
                _first.find('i').addClass('active_check').hide();
                _parents.find("i").addClass("checkR").hide();
            }

        });
        // 单选
        $(document).delegate('.list_checkBox_ico,.alter_wid,.no_checkB', 'click', function() {
            var list = $(this).parent("li").children('.list_checkBox_ico');
            if (list.children(".checkR").css("display") == "none") {
                list.find("i").addClass("checkR").show();

            } else {
                list.find("i").addClass("checkR").hide();
            }

        }).delegate(".salaryBtn", "click", function() {
            $(".j-close").trigger("click");
            start.postAjaxReq({
                param: {
                    "id": thiz.submitId,
                    "task_id": $("#J-desc-id").data("id")
                },
                type: "POST",
                url: "/worker/paySalary",
                success: function(data) {
                    thiz._showMask(data.errmsg);

                },
                errorCb: function(data) {
                    thiz._showMask(data.errmsg);
                }
            });

        }).delegate(".j_task_alter", "click", function() {
            localStorage.setItem("isEdit", true);
            location.href = "publishTask.html";
        }).delegate(".j_task_end", "click", function() {
            thiz._alertMask("是否结束招聘？", "end"); //是否结束招聘弹窗显示
        }).delegate(".j-qiandao", "click", function() {
            thiz.getcashInfo();
            start.postAjaxReq({
                param: {
                    "id": thiz.submitId,
                    "type": thiz.userType,
                    "task_id": $("#J-desc-id").data("id"),
                    "sign": "in"
                },
                type: "GET",
                url: "/task/sign",
                success: function(data) {
                    thiz._showMask(data.errmsg, function() {
                        location.reload();
                    });

                },
                errorCb: function(data) {
                    flag = true;
                    thiz._showMask(data.errmsg);
                }
            });
        }).delegate(".j-qiantui", "click", function() {
            thiz.getcashInfo();
            start.postAjaxReq({
                param: {
                    "id": thiz.submitId,
                    "type": thiz.userType,
                    "task_id": $("#J-desc-id").data("id"),
                    "sign": "out"
                },
                type: "GET",
                url: "/task/sign",
                success: function(data) {
                    thiz._showMask(data.errmsg, function() {
                        location.reload();
                    });

                },
                errorCb: function(data) {
                    flag = true;
                    thiz._showMask(data.errmsg);
                }
            });
        });

        //发放工资----弹窗

        $(".btn_salary").click(function() {
            var obj = thiz.getcashInfo();
            if (obj.no == 0) {
                thiz._showMask("至少选择一项");
                return false;
            }
            $("#J_salary_count").html(obj.cash);
            $("#J_salaryWin").show();
        });

        //点击差号---隐藏
        $(".j-close").click(function() {
            $("#J_salaryWin").hide();
        });

    },

    bindWorkerEvent: function() {
        var obj, thiz = this,
            flag = true;

        $(document).delegate('.j_sign_in', 'click', function() {
            //签到
            var _id = $(this).attr("id");

            if (!flag) {
                return false;
            }
            flag = false;
            start.postAjaxReq({
                param: {
                    "id": thiz.manageId,
                    "type": thiz.userType,
                    "task_id": _id,
                    "sign": "in"
                },
                type: "GET",
                url: "/task/sign",
                success: function(data) {
                    flag = true;
                    thiz._showMask(data.errmsg, function() {
                        location.reload();
                    });

                },
                errorCb: function(data) {
                    flag = true;
                    thiz._showMask(data.errmsg);
                }
            });
        }).delegate(".j_sign_out", "click", function() {
            //签退
            var _id = $(this).attr("id");
            if (!flag) {
                return false;
            }
            flag = false;
            start.postAjaxReq({
                param: {
                    "id": thiz.manageId,
                    "type": thiz.userType,
                    "task_id": _id,
                    "sign": "out"
                },
                type: "GET",
                url: "/task/sign",
                success: function(data) {
                    flag = true;
                    thiz._showMask(data.errmsg, function() {
                        location.reload();
                    });
                },
                errorCb: function(data) {
                    flag = true;
                    thiz._showMask(data.errmsg);
                }
            });
        }).delegate('.j_join', 'click', function() {
            thiz._alertMask('是否加入兼职?', "join");
            obj = $(this);
        }).delegate('.j_sure', 'click', function() {
            //确认加入兼职
            $("#J_alert").remove();
            var _type = $(this).attr("type");
            if (_type == "join") {
                var _id = obj.attr("task_id"),
                    _manageId = obj.attr("mange_id"),
                    _enterId = obj.attr("enter_id");
                start.postAjaxReq({
                    param: {
                        "worker_id": thiz.manageId,
                        "task_id": _id,
                        "enterprise_id": _enterId,
                        "manage_id": _manageId
                    },
                    type: "GET",
                    url: "/worker/updateTaskJoinStatus",
                    success: function(data) {
                        thiz._showMask(data.errmsg, function() {
                            location.reload();
                        });
                        location.reload();
                    },
                    errorCb: function(data) {
                        thiz._showMask(data.errmsg);
                    }
                });
            } else if (_type == "end") {

                //结束招聘 功能待定
                start.postAjaxReq({
                    param: {
                        "id": thiz.manageId,
                        "task_id": $("#J-desc-id").data('id')
                    },
                    type: "POST",
                    url: "/task/endTask/",
                    success: function(data) {
                        thiz._showMask(data.errmsg, function() {
                            location.href = "manager-list.html";
                        });
                    },
                    errorCb: function(data) {
                        thiz._showMask(data.errmsg);
                    }
                });
            }

        }).delegate('.j_no', 'click', function() {
            $("#J_alert").remove();
        });

    },

    initBtn: function() {
        var obj, $this;
        //点击兼职列表页面--我的--签到按扭调用接口



        //同上--已完成按扭---显示的弹窗
        $(document).delegate('.j_complete', 'click', function() {
            $("#J_windows3").show();

        });
        //注----这里是点击已完成按扭弹窗的“是"要调用接口
        $(".j_confirm").delegate('.j_complete_sure', 'click', function() {
                //alert("wwwwwww");
            })
            //点击已完成按扭弹窗的“否"隐藏弹窗
        $(".j_confirm").delegate('.j_complete_no', 'click', function() {
                $("#J_windows3").hide();
            })
            //点击加入兼职显示弹窗

        //平台管理列表页面修改和结束接口
        $(document).delegate(".j_task_alter", "click", function() {
            start.postAjaxReq({
                param: {
                    "id": 2,
                    "task_id": 1
                },
                type: "POST",
                url: "/task/updateTask/",
                success: function(data) {
                    console.log(data);
                    alert("兼职创建成功");
                },
                errorCb: function(data) {
                    console.log("失败");
                }
            });
        });

        //平台管理页面确认签到和确认签退接口
        //签到
        $(".footer").delegate(".j_plat_sure", "click", function() {
            start.postAjaxReq({
                param: {
                    "id": 2,
                    "task_id": 1,
                    "type": 2,
                    "sign": "in"
                },
                type: "POST",
                url: "/task/updateTask/",
                success: function(data) {
                    console.log(data);
                    alert("签到成功");
                },
                errorCb: function(data) {
                    console.log("失败");
                }
            });
        });
        //签退
        $(".footer").delegate(".j_plat_quit", "click", function() {
            start.postAjaxReq({
                param: {
                    "id": 2,
                    "task_id": 1,
                    "type": 2,
                    "sign": "out"
                },
                type: "POST",
                url: "/task/updateTask/",
                success: function(data) {
                    console.log(data);
                    alert("签退成功");
                },
                errorCb: function(data) {
                    console.log("失败");
                }
            });
        });


    }
};
$(function() {
    main.init();
});
